package com.web.surf.sample.eshop.faces.converters;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@SessionScoped
@ManagedBean
@FacesConverter(value = "com.web.surf.Price")
public class PriceConverter implements Converter, Serializable {

	private static final long serialVersionUID = 1L;
	private PriceConverter instance;

	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		// TODO Auto-generated method stub
		return null;
	}

	public PriceConverter getInstance() {
		if (instance == null) {
			return new PriceConverter();
		}
		return instance;
	}

	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (context == null || component == null) {
			throw new NullPointerException();
		}
		if (null == value || 0 == value.toString().length()) {
			return null;
		}
		String result = "free";
		Double price = (Double) value;
		if (price < 1000000 & price > 0) {
			result = price / 1000 + " th. BYR";
		}
		if (price >= 1000000) {
			result = price / 1000000 + " mln. BYR";
		}
		return result;
	}

}
